$(function () {
    obj.init();
});

var obj = new function() {
    return {
        addAreaGroupForm: null,
        addGroupName_form: null,
        addAreaData_form: null,
        form: null,
        nameForm: null,
        areaForm: null,
        demo:null,
        subjectData: null,
        versionData: null,
        needChange: true,
        init: function () {
            this.demo = $('.SubVerTr').eq(0).clone();
            this.addAreaGroupForm = $('#addAreaGroup_form');
            this.form = new xhForm(this.addAreaGroupForm);
            this.addAreaNameForm = $('#addGroupName_form');
            this.nameForm = new xhForm(this.addAreaNameForm);
            this.addAreaData_form = $('#addAreaData_form');
            this.areaForm = new xhForm(this.addAreaData_form);
            if (groupId === '' || groupId === null){
                window.top.xhTop.showError('区域组id为空');
                return false;
            }
            this.funObj().addGroupNameData();//填充基本信息
            this.funObj().addSubVersionData();//填充学科版本信息
            this.funObj().addAreaData();//填充区域数据
            this.listener();
        },
        listener: function () {
            var $this = this;
            //学段改变
            $('#stage').unbind('change').on('change',function () {
                $this.funObj().changeStage();
            });
            //保存区域组名称
            $('#save_1').click(function(){
                $this.funObj().saveOne();
            });
            // 学科改变
            $this.addAreaGroupForm.on('change','[id^="subject_"]',function () {
                var id = $(this).attr("id").split("subject_")[1];
                if ($this.needChange){
                    $this.funObj().changeSubject(id);
                }
            });
            // 版本改变
            $this.addAreaGroupForm.on('change','[id^="version_"]',function () {
                var id = $(this).attr("id").split("version_")[1];
                if ($this.needChange){
                    $this.funObj().changeVersion(id);
                }
            });
            //添加学科版本
            $('#add').click(function () {
                $this.funObj().addAction();
            });
            //删除学科版本
            $this.addAreaGroupForm.on('click','[id^="delLine_"]',function () {
                var id = $(this).attr("id").split("delLine_")[1];
                $this.funObj().removeSubVer(id);
            });
            //重载学科版本
            $('#refresh').click(function () {
                $this.funObj().refresh();
            });
            //一键复制
            $('#copy').click(function () {
                $this.funObj().copy();
            });
            //保存学科版本信息
            $('#save_2').click(function () {
                $this.funObj().saveTwo();
            });

            //添加省份
            $('#add_3').click(function () {
                $this.funObj().addProvAction();
            });
            //删除省份
            $this.addAreaData_form.on('click','[id^="delId_"]',function () {
                var id = $(this).attr("id").split("delId_")[1];
                $this.funObj().removeProv(id);
            });
            //保存地域信息
            $('#save_3').click(function () {
                $this.funObj().saveThree();
            });
            //动作-省
            $this.addAreaData_form.on('click', 'input[id^="provId_"]',function(){
                var obj = $(this);
                var id = $(this).attr("id").split("provId_")[1];
                if(this.checked){//选中时
                    $this.addAreaData_form.find('input[id^="cityId_'+id+'_"]').not(this).prop("checked", true);
                    $this.addAreaData_form.find('input[id^="distId_'+id+'_"]').not(this).prop("checked", true);
                } else {
                    window.top.swal({
                        title: '取消选择后，所包含区域相关的学校将不再有教材版本信息',
                        showCancelButton: true,
                        cancelButtonText: '我再想想',
                        confirmButtonText: '确认取消',
                        allowOutsideClick: true,
                        showCloseButton: true,
                        showLoaderOnConfirm: true,
                        type: 'warning'
                    }).then(function(isConfirm) {
                        $this.addAreaData_form.find('input[id^="cityId_'+id+'_"]').not(this).prop("checked", false);
                        $this.addAreaData_form.find('input[id^="distId_'+id+'_"]').not(this).prop("checked", false);
                    }, function(dismiss) {
                        obj.prop('checked', true);
                    });
                }
            });
            //动作-市
            $this.addAreaData_form.on('click', 'input[id^="cityId_"]',function(){
                var obj = $(this);
                var id = $(this).attr("id").split("_");
                var city = 0;
                var check_city = 0;
                if(this.checked){//选中时
                    $this.addAreaData_form.find('input[id^="distId_'+id[1]+'_'+id[2]+'"]').not(this).prop("checked", true);
                    $this.addAreaData_form.find('[id^="cityId_'+id[1]+'"]').each(function () {
                        city++;
                        if ($(this).is(':checked')){
                            check_city++;
                        }
                    });
                    if (check_city === city){
                        $this.addAreaData_form.find('input[id="provId_'+id[1]+'"]').not(this).prop("checked", true);
                    } else {
                        $this.addAreaData_form.find('input[id="provId_'+id[1]+'"]').not(this).prop("checked", false);
                    }
                } else {
                    window.top.swal({
                        title: '取消选择后，所包含区域相关的学校将不再有教材版本信息',
                        showCancelButton: true,
                        cancelButtonText: '我再想想',
                        confirmButtonText: '确认取消',
                        allowOutsideClick: true,
                        showCloseButton: true,
                        showLoaderOnConfirm: true,
                        type: 'warning'
                    }).then(function(isConfirm) {
                        $this.addAreaData_form.find('input[id^="distId_'+id[1]+'_'+id[2]+'"]').not(this).prop("checked", false);
                        $this.addAreaData_form.find('input[id^="provId_'+id[1]+'"]').not(this).prop("checked", false);
                    }, function(dismiss) {
                        obj.prop('checked', true);
                    });
                }
            });
            //动作-区
            $this.addAreaData_form.on('click', 'input[id^="distId_"]',function(){
                var obj = $(this);
                var id_key_value = $(this).attr("id").split("_");
                var dist = 0;
                var check_dist = 0;
                var city = 0;
                var check_city = 0;
                if(!this.checked){//取消选中时
                    window.top.swal({
                        title: '取消选择后，所包含区域相关的学校将不再有教材版本信息',
                        showCancelButton: true,
                        cancelButtonText: '我再想想',
                        confirmButtonText: '确认取消',
                        allowOutsideClick: true,
                        showCloseButton: true,
                        showLoaderOnConfirm: true,
                        type: 'warning'
                    }).then(function(isConfirm) {
                            $this.addAreaData_form.find('[id^="distId_'+id_key_value[1]+'_'+id_key_value[2]+'"]').each(function () {
                                dist++;
                                if ($(this).is(':checked')){
                                    check_dist++;
                                }
                            });
                            if (check_dist === dist){
                                $this.addAreaData_form.find('input[id="cityId_'+id_key_value[1]+'_'+id_key_value[2]+'"]').not(this).prop("checked", true);
                                $this.addAreaData_form.find('[id^="cityId_'+id_key_value[1]+'"]').each(function () {
                                    city++;
                                    if ($(this).is(':checked')){
                                        check_city++;
                                    }
                                });
                                if (check_city === city){
                                    $this.addAreaData_form.find('input[id="provId_'+id_key_value[1]+'"]').not(this).prop("checked", true);
                                } else {
                                    $this.addAreaData_form.find('input[id="provId_'+id_key_value[1]+'"]').not(this).prop("checked", false);
                                }
                            } else {
                                $this.addAreaData_form.find('input[id="cityId_'+id_key_value[1]+'_'+id_key_value[2]+'"]').not(this).prop("checked", false);
                                $this.addAreaData_form.find('input[id="provId_'+id_key_value[1]+'"]').not(this).prop("checked", false);
                            }
                        },
                        function(dismiss) {
                            obj.prop('checked', true);
                        });
                } else {
                    $this.addAreaData_form.find('[id^="distId_'+id_key_value[1]+'_'+id_key_value[2]+'"]').each(function () {
                        dist++;
                        if ($(this).is(':checked')){
                            check_dist++;
                        }
                    });
                    if (check_dist === dist){
                        $this.addAreaData_form.find('input[id="cityId_'+id_key_value[1]+'_'+id_key_value[2]+'"]').not(this).prop("checked", true);
                        $this.addAreaData_form.find('[id^="cityId_'+id_key_value[1]+'"]').each(function () {
                            city++;
                            if ($(this).is(':checked')){
                                check_city++;
                            }
                        });
                        if (check_city === city){
                            $this.addAreaData_form.find('input[id="provId_'+id_key_value[1]+'"]').not(this).prop("checked", true);
                        } else {
                            $this.addAreaData_form.find('input[id="provId_'+id_key_value[1]+'"]').not(this).prop("checked", false);
                        }
                    } else {
                        $this.addAreaData_form.find('input[id="cityId_'+id_key_value[1]+'_'+id_key_value[2]+'"]').not(this).prop("checked", false);
                        $this.addAreaData_form.find('input[id="provId_'+id_key_value[1]+'"]').not(this).prop("checked", false);
                    }
                }


            });
            //展开/关闭
            $this.addAreaData_form.on('click', '[id^="open_pro_"]',function(){
                var id = $(this).attr("id").split("open_pro_")[1];
                var open_pro = $('#open_pro_'+id);
                if (open_pro.hasClass('fa fa-chevron-down')){
                    open_pro.removeClass('fa fa-chevron-down').addClass('fa fa-chevron-up');
                    $this.addAreaData_form.find('[id^="td_pro_'+id+'"]').each(function () {
                        this.style.display = 'none';
                    });
                } else {
                    open_pro.removeClass('fa fa-chevron-up').addClass('fa fa-chevron-down');
                    $this.addAreaData_form.find('[id^="td_pro_'+id+'"]').each(function () {
                        this.style.display = 'table';
                    });
                }
            });
            //展开/关闭
            $this.addAreaData_form.on('click', '[id^="open_city_"]',function(){
                var id = $(this).attr("id").split("open_city_")[1];
                var open_city = $('#open_city_'+id);
                if (open_city.hasClass('fa fa-chevron-down')){
                    open_city.removeClass('fa fa-chevron-down').addClass('fa fa-chevron-up');
                    $this.addAreaData_form.find('[id^="td_city_'+id+'"]').each(function () {
                        this.style.display = 'none';
                    });
                } else {
                    open_city.removeClass('fa fa-chevron-up').addClass('fa fa-chevron-down');
                    $this.addAreaData_form.find('[id^="td_city_'+id+'"]').each(function () {
                        this.style.display = 'table';
                    });
                }
            });
        },
        funObj: function () {
            var $this = this;
            var form = this.addAreaGroupForm;
            var nameForm = this.addAreaNameForm;
            var areaForm = this.addAreaData_form;
            return {
                addGroupNameData: function () {
                    xhExtra.ajaxRequest('res/getGroupNameData', {groupId: groupId}, {check:true,async: false}, function (data) {
                        nameForm.find('#name').val(nameData);
                        nameForm.find('#stage').val(stageId);
                    });
                },
                addSubVersionData: function () {
                    xhExtra.ajaxRequest('res/getSubVerData', {groupId: groupId}, {check:true}, function (data) {
                        $this.needChange = false;
                        $('#subject_0').select2({placeholder: '请选择学科'});
                        $('#version_0').select2({placeholder: '请选择版本'});
                        if (data.empty_sub === 1){
                            $.each(data.data, function (index, value) {
                                if (index !== 0){//非第一行
                                    var dom = $($this.demo.clone());
                                    $('.SubVerTr:last').after(dom);
                                    dom.find('[name="data[0][subjectId]"]').attr('name','data['+index+'][subjectId]').attr('id','subject_'+index);
                                    dom.find('[name="data[0][versionId]"]').attr('name','data['+index+'][versionId]').attr('id','version_'+index);
                                    dom.find('[id="grade_0"]').attr('id','grade_'+index);
                                    dom.find('[id="delTdLine_0"]').attr('id','delTdLine_'+index);
                                    dom.find('[id="delLine_0"]').attr('id','delLine_'+index);
                                    dom.attr('id','trId_'+index);
                                    document.getElementById('delTdLine_'+index).style.display = 'table-cell';
                                }
                                var subObj = $('#subject_'+index);
                                subObj.select2({placeholder: '请选择学科', data: data.subject, allowClear: true});//学科列表
                                subObj.val(value.subVerData.sub).trigger("change");//学科数据
                                var verObj = $('#version_'+index);
                                verObj.select2({placeholder: '请选择版本', data: value.version, allowClear: true});//版本列表
                                verObj.val(value.subVerData.ver).trigger("change");//版本数据
                                if (value.empty_ver !== 1){//版本为空
                                    verObj.select2({placeholder: '请选择版本'});
                                } else {
                                    verObj.select2({placeholder: '请选择版本', data: value.version, allowClear: true});//版本列表
                                    verObj.val(value.subVerData.ver).trigger("change");//版本数据
                                    if (value.empty_gra !== 1){//年级为空
                                        str = '<span style="color: grey;font-size:12px;">暂无年级数据</span>';
                                    } else {
                                        str = '';
                                        $.each(value.grade, function (id, name) {
                                            var checked = '';
                                            var exist = $.inArray(parseInt(id),value.subVerData.gra);
                                            if(exist >= 0){
                                                checked = 'checked';
                                            }
                                            str += '<label>' +
                                                '<input type="checkbox" name="data['+index+'][grade][]" '+checked+' value="' + id + '">'+ name +
                                                '</label>';
                                        });
                                    }
                                    form.find('#grade_'+index).append($(str));
                                }
                            });
                        } else {
                            var stage = nameForm.find('#stage').val();
                            xhExtra.ajaxRequest('res/getSubjectByStage', {stage: stage}, {check:true}, function (data) {
                                $('#subject_0').select2({placeholder: '请选择学科', data: data, allowClear: true});
                                $('#version_0').select2({placeholder: '请选择版本'});
                            });
                        }
                        $this.needChange = true;
                    });
                },
                addAreaData: function () {
                    xhExtra.ajaxRequest('res/getAreaDataByGroupId', {stageId:stageId, groupId: groupId}, {check:true}, function (res) {
                        if (JSON.stringify(res) !== '[]') {
                            $.each(res, function (index, data) {
                                var id = $('table.prov-class:last').attr('id');
                                if (id === undefined){
                                    id = 0;
                                } else {
                                    id = id.split("province_")[1];
                                    id = parseInt(id);
                                    id++;
                                }
                                str = '';
                                if (data !== '' ){
                                    str += '<table id="province_'+ id +'" class="prov-class" style="width: 100%;">';
                                    str += '<tr>' +
                                        '<td style="width: 100%;display: table">' +
                                        '<i id="open_pro_'+id+'" class="fa fa-chevron-down" style="padding-right: 5px;cursor: pointer;" ></i>' +
                                        '<label>';
                                    if (data.isAllExist){
                                        str += '<input style="display: inline-block;vertical-align: middle;margin-bottom: 2px" type="checkbox" disabled id="provId_'+ id +'" name="data['+id+'][province]" value="'+ data.provId +'"><span style="padding-right:5px;margin:0;vertical-align:middle;color: #D6D6D6">' + data.provName + '</span>';
                                    } else {
                                        if (data.isAllChecked){
                                            str += '<input style="display: inline-block;vertical-align: middle;margin-bottom: 2px" type="checkbox" checked id="provId_'+ id +'" name="data['+id+'][province]" value="'+ data.provId +'"><span style="padding-right:5px;margin:0;vertical-align:middle;">' + data.provName + '</span>';
                                        } else {
                                            str += '<input style="display: inline-block;vertical-align: middle;margin-bottom: 2px" type="checkbox" id="provId_'+ id +'" name="data['+id+'][province]" value="'+ data.provId +'"><span style="padding-right:5px;margin:0;vertical-align:middle;">' + data.provName + '</span>';
                                        }
                                    }
                                    str += '</label>' +
                                        '<span id="delId_'+ id +'" style="margin:0;vertical-align:middle;color: gray;padding-left: 5px;cursor: pointer;">删除</span>' +
                                        '</td>';

                                    $.each(data.cityArr, function (key, city) {
                                        str += '<td id="td_pro_'+id+'" style="width: 100%;display: table;" >' +
                                            '<table style="width: 100%;">' +
                                            '<tr>' +
                                            '<td style="width: 100%;display: table;">' +
                                            '<i id="open_city_'+id+'_'+key+'" class="fa fa-chevron-down" style="padding-right: 5px;padding-left: 20px;cursor: pointer;" ></i>' +
                                            '<label>';
                                        if (city.isAllExist){
                                            str += '<label><input type="checkbox" style="display: inline-block;vertical-align: middle;margin-bottom: 2px" disabled id="cityId_'+ id + '_'+ key +'" name="data['+id+'][city][]" value="'+ city.cityId +'"><span style="padding-right:5px;margin:0;vertical-align:middle;color: #D6D6D6">' + city.cityName + '</span></label>';
                                        } else {
                                            if (city.isAllChecked){
                                                str += '<label><input type="checkbox" style="display: inline-block;vertical-align: middle;margin-bottom: 2px" checked id="cityId_'+ id + '_'+ key +'" name="data['+id+'][city][]" value="'+ city.cityId +'"><span style="padding-right:5px;margin:0;vertical-align:middle;">' + city.cityName + '</span></label>';
                                            } else {
                                                str += '<label><input type="checkbox" style="display: inline-block;vertical-align: middle;margin-bottom: 2px" id="cityId_'+ id + '_'+ key +'" name="data['+id+'][city][]" value="'+ city.cityId +'"><span style="padding-right:5px;margin:0;vertical-align:middle;">' + city.cityName + '</span></label>';
                                            }
                                        }
                                        str += '</label>' +
                                            '</td>';
                                        str += '<td id="td_city_'+id+'_'+key+'" style="width: 100%;display: table">' +
                                            '<div style="width: 100%; padding-left: 60px">';
                                        $.each(city.distArr, function (value, dist) {
                                            var title = '';
                                            var isTooLang = $this.funObj().checkCNWordNum(dist.distName);//区字数超过3个省略显示点
                                            if (isTooLang){
                                                title = dist.distName;
                                            }
                                            if (dist.isExist){
                                                str += '<label title="'+title+'" style="width: 70px;white-space: nowrap;overflow: hidden;text-overflow: ellipsis;"><input style="display: inline-block;vertical-align: middle;margin-bottom: 2px" type="checkbox" name="data['+id+'][dist][]" disabled value="'+ dist.distId +'"><span style="padding-right:5px;margin:0;vertical-align:middle;color: #D6D6D6">' + dist.distName + '</span></label>';
                                            } else {
                                                if (dist.isChecked){
                                                    str += '<label title="'+title+'" style="width: 70px;white-space: nowrap;overflow: hidden;text-overflow: ellipsis;"><input style="display: inline-block;vertical-align: middle;margin-bottom: 2px" type="checkbox" checked id="distId_'+ id + '_'+ key + '_' + value +'" name="data['+id+'][dist][]"  value="'+ dist.distId +'"><span style="padding-right: 5px;margin:0;vertical-align:middle;color: black">' + dist.distName + '</span></label>';
                                                } else {
                                                    str += '<label title="'+title+'" style="width: 70px;white-space: nowrap;overflow: hidden;text-overflow: ellipsis;"><input style="display: inline-block;vertical-align: middle;margin-bottom: 2px" type="checkbox" id="distId_'+ id + '_'+ key + '_' + value +'" name="data['+id+'][dist][]"  value="'+ dist.distId +'"><span style="padding-right: 5px;margin:0;vertical-align:middle;color: black">' + dist.distName + '</span></label>';
                                                }
                                            }
                                        });
                                        str += '</div>' +
                                            '</td>' +
                                            '</tr>' +
                                            '</table>' +
                                            '</td>';
                                    });
                                    str += '</tr>' +
                                        '</table>';
                                }
                                areaForm.find('#form_div').append($(str));
                            });
                        }
                    })
                },
                checkCNWordNum: function (search) {
                    find = /[\u4E00-\u9FA5]/g;
                    if (find.test(search) && search.match(find).length > 3){//如果是中文不能少于四个字
                        return true;
                    } else {
                        return false;
                    }
                },
                addProvAction: function () {
                    var existProv = [];
                    areaForm.find('[id^="provId_"]').each(function () {
                        existProv.push($(this).val());
                    });
                    window.top.xhTop.showModal({
                        title: '选择省份',
                        content: xhTool.getFrameString('res/setProvPage', {existProv:existProv}, 380),
                        button: '确定',
                        size: 'sm',
                        width:'800'
                    });
                },
                addProvData: function (provId) {
                    var stageId = nameForm.find('#stage').val();
                    xhExtra.ajaxRequest('res/getAreaDataByProv', {stageId:stageId, provId: provId, groupId: groupId}, {check:true, async:false}, function (data) {
                        var id = $('table.prov-class:last').attr('id');
                        if (id === undefined){
                            id = 0;
                        } else {
                            id = id.split("province_")[1];
                            id = parseInt(id);
                            id++;
                        }
                        str = '';
                        if (data !== '' ){
                            str += '<table id="province_'+ id +'" class="prov-class" style="width: 100%;">';
                            str += '<tr>' +
                                '<td style="width: 100%;display: table">' +
                                '<i id="open_pro_'+id+'" class="fa fa-chevron-down" style="padding-right: 5px;cursor: pointer;" ></i>' +
                                '<label>';
                            if (data.isAllExist){
                                str += '<input style="display:inline-block; vertical-align:middle;margin-bottom: 2px" type="checkbox" id="provId_'+ id +'" name="data['+id+'][province]" disabled value="'+ data.provId +'"><span style="color: #D6D6D6;padding-right:5px;margin:0;vertical-align:middle;">'+ data.provName +'</span>';
                            } else {
                                if (data.isAllChecked){
                                    str += '<input style="display:inline-block; vertical-align:middle;margin-bottom: 2px" type="checkbox" checked id="provId_'+ id +'" name="data['+id+'][province]" value="'+ data.provId +'"><span style="padding-right:5px;margin:0;vertical-align:middle;">' + data.provName + '</span>';
                                } else {
                                    str += '<input style="display:inline-block; vertical-align:middle;margin-bottom: 2px" type="checkbox" id="provId_'+ id +'" name="data['+id+'][province]" value="'+ data.provId +'"><span style="padding-right:5px;margin:0;vertical-align:middle;">' + data.provName + '</span>';
                                }
                            }
                            str += '</label>' +
                                '<span id="delId_'+ id +'" style="margin:0;vertical-align:middle;color: gray;padding-left: 5px;cursor: pointer;">删除</span>' +
                                '</td>';

                            $.each(data.cityArr, function (key, city) {
                                str += '<td id="td_pro_'+id+'" style="width: 100%;display: table;" >' +
                                    '<table style="width: 100%;">' +
                                    '<tr>' +
                                    '<td style="width: 100%;display: table;">' +
                                    '<i id="open_city_'+id+'_'+key+'" class="fa fa-chevron-down" style="padding-right: 5px;padding-left: 20px;cursor: pointer;" ></i>' +
                                    '<label>';
                                if (city.isAllExist){
                                    str += '<input style="display: inline-block;vertical-align: middle;margin-bottom: 2px" type="checkbox" name="data['+id+'][city][]" disabled value="'+ city.cityId +'"><span style="margin:0;vertical-align:middle;color: #D6D6D6">'+ city.cityName +'</span>';
                                } else {
                                    if (city.isAllChecked){
                                        str += '<input style="display: inline-block;vertical-align: middle;margin-bottom: 2px" type="checkbox" checked id="cityId_'+ id + '_'+ key +'" name="data['+id+'][city][]" value="'+ city.cityId +'"><span style="margin:0;vertical-align:middle;">'+ city.cityName +'</span>';
                                    } else {
                                        str += '<input style="display: inline-block;vertical-align: middle;margin-bottom: 2px" type="checkbox" id="cityId_'+ id + '_'+ key +'" name="data['+id+'][city][]" value="'+ city.cityId +'"><span style="margin:0;vertical-align:middle;">'+ city.cityName +'</span>';
                                    }
                                }
                                str += '</label>' +
                                    '</td>';
                                str += '<td id="td_city_'+id+'_'+key+'" style="width: 100%;display: table">' +
                                    '<div style="width: 100%; padding-left: 60px">';
                                $.each(city.distArr, function (value, dist) {
                                    var title = '';
                                    var isTooLang = $this.funObj().checkCNWordNum(dist.distName);//区字数超过3个省略显示点
                                    if (isTooLang){
                                        title = dist.distName;
                                    }
                                    if (dist.isExist){
                                        str += '<label title="'+title+'" style="width: 70px;white-space: nowrap;overflow: hidden;text-overflow: ellipsis;"><input type="checkbox" style="display: inline-block;vertical-align: middle;margin-bottom: 2px" type="checkbox" name="data['+id+'][dist][]" disabled value="'+ dist.distId +'"><span style="padding-right:5px;margin:0;vertical-align:middle;color: #D6D6D6">' + dist.distName + '</span></label>';
                                    } else {
                                        if (dist.isChecked){
                                            str += '<label title="'+title+'" style="width: 70px;white-space: nowrap;overflow: hidden;text-overflow: ellipsis;"><input type="checkbox" style="display: inline-block;vertical-align: middle;margin-bottom: 2px" type="checkbox" checked id="distId_'+ id + '_'+ key + '_' + value +'" name="data['+id+'][dist][]"  value="'+ dist.distId +'"><span style="padding-right: 5px;margin:0;vertical-align:middle;color: black">' + dist.distName + '</span></label>';
                                        } else {
                                            str += '<label title="'+title+'" style="width: 70px;white-space: nowrap;overflow: hidden;text-overflow: ellipsis;"><input type="checkbox" style="display: inline-block;vertical-align: middle;margin-bottom: 2px" type="checkbox" id="distId_'+ id + '_'+ key + '_' + value +'" name="data['+id+'][dist][]"  value="'+ dist.distId +'"><span style="padding-right: 5px;margin:0;vertical-align:middle;color: black">' + dist.distName + '</span></label>';
                                        }
                                    }
                                });
                                str += '</div>' +
                                    '</td>' +
                                    '</tr>' +
                                    '</table>' +
                                    '</td>';
                            });
                            str += '</tr>' +
                                '</table>';
                        }
                        areaForm.find('#form_div').append($(str));
                    });
                },
                removeProv: function (id) {
                    window.top.swal({
                        title: '确认移除该省',
                        showCancelButton: true,
                        cancelButtonText: '取消',
                        confirmButtonText: '确认',
                        allowOutsideClick: true,
                        showCloseButton: true,
                        showLoaderOnConfirm: true,
                        type: 'warning'
                    }).then(function(isConfirm) {
                        areaForm.find('[id=province_'+id+']').remove();
                    }, function(dismiss) {
                    });
                },
                removeSubVer: function (id) {
                    form.find('[id=trId_'+id+']').remove();
                },
                changeStage: function () {
                    var stage = nameForm.find('#stage').val();
                    $this.funObj().delOptions();
                    if (parseInt(stage) === parseInt(stageId)){//学段与原来相同
                        $this.funObj().addSubVersionData();
                        $('#save_2').removeClass('disabled');
                        $this.funObj().addAreaData();
                        document.getElementById('showCreateArea').style.display = 'contents';
                        $('#save_3').removeClass('disabled');
                    } else {
                        xhExtra.ajaxRequest('res/getSubjectByStage', {stage: stage}, {check:true}, function (data) {
                            $('#subject_0').select2({placeholder: '请选择学科', data: data, allowClear: true});
                            $('#version_0').select2({placeholder: '请选择版本'});
                        });
                        areaForm.find('#form_div').html('');
                        $('#save_2').addClass('disabled');
                        $('#save_3').addClass('disabled');
                        document.getElementById('showCreateArea').style.display = 'none';
                    }
                },
                delOptions: function () {
                    $("#subject_0").find("option").remove();
                    $("#version_0").find("option").remove();
                    form.find('#grade_0').html('');
                    var dom = $('.SubVerTr');
                    var tr_length = dom.length;
                    var i = 1;
                    if (tr_length > 1){
                        while (i <= tr_length-1){
                            form.find('[id=trId_'+i+']').remove();
                            i++;
                        }
                    }
                },
                saveOne: function () {
                    var name = nameForm.find('#name').val();
                    var stage = nameForm.find('#stage').val();
                    if (parseInt(stage) !== parseInt(stageId)){//学段和以前不同
                        document.getElementById('showCreateArea').style.display = 'contents';
                    }
                    if (name !== '' && stage !== '' && groupId !== ''){
                        xhExtra.ajaxRequest('res/saveGroupInfo', {name:name,stage:stage,groupId:groupId}, {check:true}, function (data) {
                            if (data.type === 1){
                                $('#notice').html(data.msg);
                                return false;
                            } else {
                                $('#notice').html('');
                                stageId = stage;
                                window.top.xhTop.showSuccess('成功');
                                $('#save_2').removeClass('disabled');
                                $('#save_3').removeClass('disabled');
                                $this.funObj().reloadListData();
                            }
                        });
                    } else {
                        $('#notice').html('请填写名称和学段');
                        return false;
                    }
                },
                changeSubject: function (id) {
                    var stage = nameForm.find('#stage').val();
                    var subject = form.find('#subject_'+id).val();
                    var exist = $this.funObj().getExistDataArr(subject);
                    xhExtra.ajaxRequest('res/getVersionBySub', {stage: stage, subject: subject}, {check:true}, function (data) {
                        var obj = $('#version_'+id);
                        obj.find("option").remove();
                        obj.select2({placeholder: '请选择版本', data: data, allowClear: true});
                        if (JSON.stringify(exist) !== '[]'){
                            exist.forEach(function(value,index){
                                form.find("#version_"+id+" option[value="+value+"]").prop('disabled',true);
                            });
                            obj.select2({placeholder: '请选择版本'});
                        }
                    });
                    form.find('#grade_'+id).html('');
                },
                getExistDataArr: function (subj_data) {
                    var arr = [];
                    $this.addAreaGroupForm.find('[id^="subject_"]').each(function () {
                        var id = $(this).attr("id").split("subject_")[1];
                        var subject = form.find('#subject_'+id).val();
                        if (subject == subj_data){
                            var version = form.find('#version_'+id).val();
                            if (version){
                                arr.push(version);
                            }
                        }
                    });
                    return arr;
                },
                changeVersion: function (id) {
                    var subject = form.find('#subject_'+id).val();
                    var version = form.find('#version_'+id).val();
                    $this.funObj().addGrade(id, subject, version);
                },
                addAction : function () {
                    var dom = $($this.demo.clone());
                    var stage = nameForm.find('#stage').val();
                    $('.SubVerTr:last').after(dom);
                    // var index = $('.SubVerTr').length - 1 ;
                    var index = 0;
                    $this.addAreaGroupForm.find('[id^="trId_"]').each(function () {
                        var id = $(this).attr("id").split("trId_")[1];
                        if (index < parseInt(id)){
                            index = parseInt(id);
                        }
                    });
                    index++;
                    dom.find('[name="data[0][subjectId]"]').attr('name','data['+index+'][subjectId]').attr('id','subject_'+index);
                    dom.find('[name="data[0][versionId]"]').attr('name','data['+index+'][versionId]').attr('id','version_'+index);
                    dom.find('[id="grade_0"]').attr('id','grade_'+index);
                    dom.find('[id="delTdLine_0"]').attr('id','delTdLine_'+index);
                    dom.find('[id="delLine_0"]').attr('id','delLine_'+index);
                    dom.attr('id','trId_'+index);
                    document.getElementById('delTdLine_'+index).style.display = 'table-cell';
                    $('#subject_'+index).select2({placeholder: '数据加载中..'});
                    $('#version_'+index).select2({placeholder: '数据加载中..'});
                    xhExtra.ajaxRequest('res/getSubjectByStage', {stage: stage}, {check:true}, function (data) {
                        $('#subject_'+index).select2({placeholder: '请选择学科', data: data, allowClear: true});
                        $('#version_'+index).select2({placeholder: '请选择版本'});
                    });
                },
                refresh: function () {
                    nameForm.find('#stage').val(stageId);

                    $this.funObj().delOptions();
                    $this.funObj().addSubVersionData();
                },
                copy: function () {
                    window.top.swal({
                        showCancelButton: true,
                        confirmButtonText: '复制并新建',
                        cancelButtonText: '取消',
                        showLoaderOnConfirm: true,
                        allowOutsideClick: false,
                        title: '复制学科版本关联并新建区域组',
                        html: '<div>新建区域组名：<input id="newGroupName" class="swal2-input" placeholder="必填">' +
                        '<\div>',
                        preConfirm: function() {
                            return new Promise(function (resolve, reject) {
                                var newGroupName = $('#newGroupName', window.top.document).val();
                                if(newGroupName === '' || newGroupName === null || newGroupName === undefined){
                                    reject('请填写新组名');
                                } else {
                                    $this.form.submit('res/copyCreateGroup',{groupName:newGroupName,stageId:stageId},function (res) {
                                    },{check:true,success:true});
                                    resolve();
                                    $this.dataGrid.reload();
                                }
                            })
                        }
                    })
                },
                addGrade: function (id, subject, version) {
                    var stage = nameForm.find('#stage').val();
                    form.find('#grade_'+id).html('');
                    if (version !== null && version !== '' && version !== undefined){
                        xhExtra.ajaxRequest('res/getGradeBySubVer', {subject: subject, version: version,stage: stage}, {check:true, async:false}, function (data) {
                            if (data !== '' ){
                                str = '';
                                $.each(data, function (value, name) {
                                    str += '<label>' +
                                        '<input type="checkbox" name="data['+id+'][grade][]" checked="checked" value="' + value + '">'+ name +
                                        '</label>';
                                });
                            } else {
                                str = '<span style="color: grey;font-size:12px;">暂无数据</span>';
                            }
                            form.find('#grade_'+id).append($(str));
                        });
                    }
                },
                saveTwo: function () {
                    var stage = nameForm.find('#stage').val();
                    if (parseInt(stage) !== parseInt(stageId)){
                        window.top.xhTop.showError('名称和学段修改未保存，请先保存');
                        return false;
                    }
                    $this.form.submit('res/saveSubjectVersion',{},function (res) {},{check:true,success:true});
                    $this.funObj().reloadListData();
                },
                saveThree: function () {
                    $this.areaForm.submit('res/saveAreaData',{groupId:groupId},function (res) {
                        if(xhExtra.checkError(res)){
                            return false;
                        }
                        window.top.xhTop.showSuccess(res.msg);
                        $this.funObj().reloadListData();
                    },{check:false});
                },
                reloadListData: function () {
                    var beforeFrame = window.top.xhTop.getFrameByName('区域组');
                    if(beforeFrame){
                        beforeFrame.obj.funObj().reloadList();
                    }
                }
            }
        }
    }
};
